package com.course.business.controller.admin;

import com.course.server.dto.SectionDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.dto.SectionPageDto;
import com.course.server.service.SectionService;
import com.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/section")
public class SectionController {

    private static final Logger LOG = LoggerFactory.getLogger(SectionController.class);
    public static final String BUSINESS_NAME = "小节";

    @Autowired
    private SectionService sectionService;

    @RequestMapping("/list")
    public ResponseDto<SectionPageDto> test(@RequestBody SectionPageDto sectionPageDto) {
        LOG.info("sectionPageDto:{}", sectionPageDto);
        sectionService.list(sectionPageDto);
        return new ResponseDto <>(sectionPageDto);
    }

    @RequestMapping("/save")
    public ResponseDto<SectionDto> save(@RequestBody SectionDto sectionDto) {
        LOG.info("sectionDto:{}", sectionDto);
        // 保存校验
        ValidatorUtil.require(sectionDto.getId(), "ID");
        ValidatorUtil.require(sectionDto.getTitle(), "标题");
        ValidatorUtil.length(sectionDto.getTitle(), "标题", 1 , 50);
        ValidatorUtil.length(sectionDto.getVideo(), "视屏", 1 , 200);

        sectionService.save(sectionDto);
        return new ResponseDto <>(sectionDto);
    }

    @RequestMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        sectionService.del(id);
        return new ResponseDto<>("200","删除成功！");
    }

}